<template>
	<div class="work-station-container">
		<a-row :gutter="16">
			<!-- banner -->
			<a-col :span="16" class="banner-box">
				<img class="banner" :src="BannerIcon2" />
			</a-col>
			<!-- 用户卡片 -->
			<a-col :span="8">
				<UserCard  :uinfo="userInfoRef" :isnormaluser="true"/>
			</a-col>  
		</a-row> 
        <a-row :gutter="16" style="margin-top: 16px;"> 
			<!-- 报修动态 -->
			<a-col :span="24" > 
                <RepairDongTai :dtdata="dongtai" :isnormaluser="true"/>    
            </a-col>
		</a-row> 
	</div>
    <fotter style="margin-bottom: -10px;"/> 
</template>
<script setup>
	import { computed, ref, onMounted, reactive, h } from 'vue';
	import { useRouter } from 'vue-router';
	import { useUserStore } from '@/store/modules/user';
	import tool from '@/utils/tool';
	import { notification } from 'ant-design-vue';
	import { APP_CONFIG } from '@/constants/system-const';
    
    import RepairDongTai from '@/views/work-station/repair-dongtai.vue';  
	import UserCard from '@/views/work-station/user-card.vue';
	import BannerIcon2 from '@/assets/images/home/banner2.png';
    import fotter from '@/layout/fotter/fotter.vue';
	import { RepairApi } from '@/api/repair-api';
  
	const userInfoRef = ref({});
	const tognjiDataRef = ref({});
	const dongtai = ref({});

	const initData = async function(){
		tool.showLoading();
		const res = await RepairApi.getUserWorkStation();
		tool.hideLoading();
		if(res.code!='0'){
			tool.error(res.codemessage);
			return;
		}
		let userInfo = {
			userName: res.姓名,
			os_l_time: res.上次登录时间[0].os_l_time,
			departmentName:useUserStore().departmentName
		};
		userInfoRef.value = userInfo;
		
		let data06 = getData06(res.data06);
		let tjData = {
			data01:res.data01&&res.data01.length>0?res.data01[0].size_num:0,
			data02:res.data02&&res.data02.length>0?res.data02[0].size_num:0,
			data03:res.data03&&res.data03.length>0?res.data03[0].size_num:0,
			data04:res.data04&&res.data04.length>0?res.data04[0].size_num:0,
			data05:res.data05&&res.data05.length>0?res.data05[0].size_num:0,
			data06,
		} 
		tognjiDataRef.value = tjData;

		let data07 = {
			data1: res.我发起的,
			data2: res.我处理的,
		} 
		dongtai.value = data07;
	}
	const getData06 = function(datas){
		let  dates = getMonths();
		let lastData =[];
		dates.lastYears.forEach(item => {
			let d =  datas.find(val => {
				return val.rep_wo_insert_time == item;
			});
			if(d){
				lastData.push(d.size_num);
			}else{
				lastData.push(0);
			}	
		});
		let thisData =[];
		var currentTime = new Date();
		let month = currentTime.getMonth();
		month = month+1;
		dates.thisYears.forEach(item => {
			let tempMonth = item.split('-')[1] * 1;
			if(month<tempMonth){
				return;
			}
			let d =  datas.find(val => {
				return val.rep_wo_insert_time == item;
			});
			if(d){
				thisData.push(d.size_num);
			}else{
				thisData.push(0);
			}	
		});
		return {
			lastYear:dates.lastYear,
			thisYear:dates.thisYear,
			lastData,
			thisData
		}
	}
	const getMonths = function(){
		var currentTime = new Date();
		var previousYear = currentTime.getFullYear() - 1;  
		var dates = {
			thisYear: previousYear+1,
			lastYear:previousYear,
			lastYears:[],
			thisYears:[],
		};
		for(let i=1;i<=12;i++){
			let month  = i<10?('0'+i):i;
			dates.lastYears.push(previousYear+"-"+month)
			dates.thisYears.push((previousYear+1)+"-"+month)
		}
		return dates;
	}
   
	onMounted(() => {
		initData();
		notification['success']({
			message: '温馨提示',
			description: '您好！欢迎您访问' + APP_CONFIG.websiteName + '！',
			duration: 4,
		});
	});
</script>
<style lang="less" scoped>
	.work-station-container {
    width: 70%;
    min-width: 800px;
    margin: 0 auto;
	min-height:calc(100% - 118px);
    .banner {
        width: 100%;
        height: 300px;
        object-fit: fill;
        border-radius: 4px;
    }
} 
</style>
